@extends('back.adminbase')
@section('content')
    <section class="hbox stretch">
        @include("back.public.left")
        <aside>
            <section class="vbox">

                <header class="header bg-white b-b clearfix">
                    <form class="talbe-search" method="get" action="">
                        <div class="row m-t-sm">
                            <div class="col-sm-12 m-b-xs">
                                <div class="btn-group" role="group">
                                    <strong style="line-height:30px;">编辑漂流柜</strong>
                                </div>
                                <div class="btn-group pull-right">
                                    <a role="button" class="btn btn-info btn-sm " title="返回" onclick="history.back()">
                                        <i class="glyphicon glyphicon-arrow-left"></i>&nbsp;返回
                                    </a>
                                </div>
                            </div>
                        </div>
                    </form>
                </header>
                <section class="scrollable  wrapper w-f">
                    <section class="panel panel-default ">
                        <div class="row m-l-none m-r-none bg-light lter">
                            <div class="container" style="padding: 20px 0px">
                                <form id="formclass" class="form-horizontal" style="padding-top: 15px;">
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">柜名<span class="red">*</span></label>
                                        <div class="col-sm-4 ">
                                            <input type="type" class="form-control" required="required" name="name" value="{{$res->name}}">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">类型<span class="red"></span></label>
                                        <div class="col-sm-4 ">
                                            <select name='type' class="input-sm">
                                                @foreach([1=>'漂流柜',2=>'还书箱'] as $k=>$v)
                                                    <option value="{{ $k}}" @if($res->type==$k) selected @endif>{{ $v }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">地区 </label>
                                        <div class="col-sm-4 ">
                                            <select id="region_id" class="input-sm">
                                                @foreach($parent as $v)
                                                    <option value="{{ $v->id }}"  @if($res->pid==$v->id) selected @endif>{{ $v->name }}</option>
                                                @endforeach
                                            </select>
                                            <select id="region_id2" class="input-sm">
                                                @foreach($child as $v)
                                                    <option value="{{ $v->id }}" @if($res->chid==$v->id) selected @endif>{{ $v->name }}</option>
                                                @endforeach
                                            </select>
                                            <select id="region_id3" class="input-sm" name="cid">
                                                @foreach($qu as $v)
                                                    <option value="{{ $v->id }}" @if($res->cid==$v->id) selected @endif>{{ $v->name }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">地址<span class="red">*</span></label>
                                        <div class="col-sm-4 ">
                                            <input type="text" class="form-control" required="required" name="address" value="{{$res->address}}">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">经度<span class="red">*</span></label>
                                        <div class="col-sm-4 ">
                                            <input type="text" class="form-control" required="required" name="longi" value="{{$res->longi}}">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">纬度<span class="red">*</span></label>
                                        <div class="col-sm-4 ">
                                            <input type="text" class="form-control" required="required" name="lati" value="{{$res->lati}}">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"></label>
                                        <div class="col-sm-2 ">
                                            <button role="button" class="btn btn-default submit" type="submit">提交</button>
                                            <input type="checkbox" id="back">
                                            <label for="back">返回列表</label>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </section>
                </section>
            </section>
        </aside>
    </section>
@endsection
@section('addscript')
    <script>
        $(function(){
            //二级联动
            $("#region_id").change(function() {
                var first = $(this).val();
                $.post("{{ url('back/book/pull') }}",{pid:first},function(data){
                    if(data){
                        var region_id2 = $("#region_id2");
                        $("option",region_id2).remove();
                        $.each(data,function(index,array){
                            var option = "<option value='"+array['id']+"'>"+array['name']+"</option>";
                            region_id2.append(option);
                        });
                        var first2 = $('#region_id2').val();
                        $.post("{{ url('back/book/pull') }}",{pid:first2},function(data){
                            if(data){
                                var region_id3 = $("#region_id3");
                                $("option",region_id3).remove();
                                $.each(data,function(index,array){
                                    var option = "<option value='"+array['id']+"'>"+array['name']+"</option>";
                                    region_id3.append(option);
                                });
                            }
                        });
                    }
                });
            });
            //二级联动
            $("#region_id2").change(function() {
                var first = $(this).val();
                $.post("{{ url('back/book/pull3') }}",{pid:first},function(data){
                    if(data){
                        var region_id3 = $("#region_id3");
                        $("option",region_id3).remove();
                        $.each(data,function(index,array){
                            var option = "<option value='"+array['id']+"'>"+array['name']+"</option>";
                            region_id3.append(option);
                        });
                    }
                });
            });

            $("#photo_all").change(function(){
                var id="photo_all";
                changePics(id);
            });

            //上传单图
            $("#input_file1").change(function(){
                changePic("input_file1","file_img1","input_hidden1")
            });

            //上传单图
            $("#input_file").change(function(){
                changePic("input_file","file_img","input_hidden")
            });

            //提交表单
            $("#formclass").submit(function(){
                var data = $(this).serialize();
                var back=$("#back").is(':checked');
                $.ajax({
                    url:"{{ url('back/shelf/edit/'.$id) }}",
                    type:"post",
                    dataType:"json",
                    data:data,
                    success:function(data){
                        if(data.success){
                            layer.alert(data.msg,function(){
                                if (back) {
                                    location.href="{{url('back/shelf')}}";
                                } else {
                                    location.reload();
                                }
                            });
                        }
                        else{
                            layer.alert(data.msg);
                        }
                    }
                })
                return false;
            });
        });
    </script>
@endsection
